package com.kaili.common.util;

import com.kaili.common.pojo.entity.BaseEmployee;
import com.kaili.common.pojo.entity.BaseUser;
import com.kaili.common.pojo.entity.LoginDetail;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Locale;

public class LoginDetailUtils {
    public static LoginDetail getCurrent() {
        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal().getClass() == BaseUser.class){
            return (BaseUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }
        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal().getClass() == BaseEmployee.class){
            return (BaseEmployee)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }
        return null;
    }

    public static Integer getCurrentEmployeeId() {
        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal().getClass() == BaseUser.class){
            return ((BaseUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getEmployeeId();
        }
        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal().getClass() == BaseEmployee.class){
            return ((BaseEmployee)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        }
        return null;
    }

    public static String getUserName(){
        try {
            return ((BaseUser)LoginDetailUtils.getCurrent()).getName();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取网点Id
     * @return
     */
    public static Integer getSiteId(){
        try {
            return ((BaseUser)LoginDetailUtils.getCurrent()).getSiteId();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取网点编码
     *
     * @return
     */
    public static String getSiteCode() {
        try {
            return ((BaseUser) LoginDetailUtils.getCurrent()).getSiteCode();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取网点名称
     * @return
     */
    public static String getSiteName(){
        try {
            return ((BaseUser)LoginDetailUtils.getCurrent()).getSiteName();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取当前用户选择的语言
     *
     * @return
     */
    public static Locale getCurrentUserLocale() {
        return Locale.SIMPLIFIED_CHINESE;
    }

    /**
     * 获取公司名称
     * @return
     */
    public static String getCompanyName(){
        try {
            return ((BaseUser)LoginDetailUtils.getCurrent()).getCompanyName();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取公司编码
     * @return
     */
    public static String getCompanyCode(){
        try {
            return ((BaseUser)LoginDetailUtils.getCurrent()).getCode();
        } catch (Exception e) {
            return null;
        }
    }


}
